package com.smiier.skin;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import cn.o.app.event.listener.OnSelectedChangeListener;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.OFragmentFrame;
import cn.o.app.ui.OTab;

import com.evan.common.share.ShareQQUtils;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.SysGetVerTask;
import com.smiier.skin.net.SysGetVerTask.SysGetVerRequest;
import com.smiier.skin.net.SysGetVerTask.SysGetVerResponse;
import com.smiier.skin.net.SysGetVerTask.SysVerList;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.ui.PersonalDoctorFragment;
import com.smiier.skin.ui.SessionFragment;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

/**
 * 
 * @author Administrator
 *
 */
public class NavigateActivity extends BasicActivity {
	protected OFragmentFrame mFrame;
	protected OTab mTab;

	protected int mLastInterceptTabIndex = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_navigate);
		GlobalSettings.isDoctor = true;

		// 检查版本更新
		checkVersion(true, null);

		/**
		 * 数据库更新
		 */

		doCheckDataVersion();

		mFrame = findViewById(R.id.frame, OFragmentFrame.class);
		mTab = findViewById(R.id.tab, OTab.class);

		SessionFragment sessionFragment = findViewById(R.id.session_fragment,
				SessionFragment.class);
		sessionFragment.setBageViewTarget(mTab.getChildAt(1));
		IntentFilter filter = new IntentFilter("send");
		registerReceiver(xxReceiver, filter);

		PersonalDoctorFragment patientFragment = findViewById(
				R.id.personal_fragment, PersonalDoctorFragment.class);
		patientFragment.setBageViewTarget(mTab.getChildAt(3));

		mTab.setOnSelectedChangeListener(new OnSelectedChangeListener() {

			@Override
			public boolean onInterceptChange(View v, int index) {
				if (GlobalSettings.sUser == null
						|| GlobalSettings.sToken == null) {
					startActivity(new Intent(getContext(), LoginActivity.class));
					mLastInterceptTabIndex = index;
					return true;
				}
				if (index == 2) {
					mLastInterceptTabIndex = -1;
					return false;
				}

				if (CommonUtility.isDoctor()) {
					if (index == 3) {
						if (GlobalSettings.sUser.Is_Cert != 1
								&& GlobalSettings.sUser.Is_Cert != 3) {

							Log.i("testcallback", "NavigateActivity");

							Intent intent = new Intent(NavigateActivity.this,
									DoctorProfileEditActivity.class);
							intent.putExtra(Constant.IDENTITY_KEY, true);
							startActivity(intent);
							mLastInterceptTabIndex = index;
							return true;
						}
					}
				}
				mLastInterceptTabIndex = -1;
				return false;
			}

			@Override
			public void onChanged(View v, int index) {
				mFrame.setSelectedIndex(index);
			}
		});
		
		
			mTab.setSelectedIndex(2);
			mFrame.setSelectedIndex(2);
		
		if (GlobalSettings.sUser == null || GlobalSettings.sToken == null) {
			startActivity(new Intent(getContext(), LoginActivity.class));
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		
		if (GlobalSettings.sUser != null) {
			if (mLastInterceptTabIndex != -1) {
				mTab.setSelectedIndex(mLastInterceptTabIndex);
			}
		}
		
		
		if(GlobalSettings.isBangding){
			mTab.setSelectedIndex(3);
			GlobalSettings.isBangding = false;
		}
		
		
	}

	// 检查固化数据版本
	protected void doCheckDataVersion() {
		SysGetVerRequest req = new SysGetVerRequest();
		SysGetVerTask task = new SysGetVerTask();
		task.setRequest(req);
		task.addListener(new NetTaskListener<SysGetVerTask.SysGetVerRequest, SysGetVerTask.SysGetVerResponse>() {

			@Override
			public void onException(
					INetTask<SysGetVerRequest, SysGetVerResponse> task,
					Exception e) {

			}

			@Override
			public void onComplete(
					INetTask<SysGetVerRequest, SysGetVerResponse> task,
					SysGetVerResponse response) {
				if (response.ResultCode != 200) {
					return;
				}
				if (response.Res == null) {
					return;
				}

				SysVerList cacheSysVerList = GlobalSettings
						.getSysVerList(getContext());
				if (response.Res.equals(cacheSysVerList)) {
					return;
				}
				GlobalSettings.setSysVerList(getContext(), response.Res);
			}
		});
		add(task);
	}

	@Override
	public boolean onInterceptBackPressed() {
		if (super.onInterceptBackPressed()) {
			return true;
		}
		this.moveTaskToBack(true);
		return true;
	}

	BroadcastReceiver xxReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(NavigateActivity.this, "收到信息", 0).show();
		}
	};

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (null != ShareQQUtils.getInstance(this).getTencent()) {
			ShareQQUtils.getInstance(this).getTencent()
					.onActivityResult(requestCode, resultCode, data);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		CommonUtility.unRegisteReciver((Activity) getContext(), xxReceiver);
	}

}